<?php
namespace logic\set\tags;
/**
 * 标签管理
 */
class TagsManage extends \logic\Base{
    
    
    public function  add($param){
        return D('Tags','set')->add($param);
    }
    
    
    public function getList($param){
        return D('Tags','set')->getSimpleList($param);
    }
    
    /**
     * 修改全部
     */
    public function editAll($param){
        $list =  D('Tags','set')->getSimpleList(['siteid'=> $this->getSiteId()]);
        $num = count($list);
        foreach( $param as $key => $val ){
            foreach( $list as $okey => $oval ){
                if( $val['id'] == $oval['id']){
                    $data[] = [
                        'id'        => $val['id'],
                        'tagname'   => $val['tagname'],
                        'sort'      => $num-$key,
                        'status'    => $val['status']
                    ];
                }
            }
        }
        try{
            $model = M();
            $model->startTrans();
            foreach( $data as $li ){
                D('Tags','set')->edit($li['id'],$li);
            }
            $model->commit();
        }catch( \think\Exception $e){
            $model->rollBack();
            logicE($e->getMessage());
        }
        return true;
    }
    
    /**
     * 修改状态
     */
    public function editStatus($id){
        $info = D('Tags','set')->find(['id'=>$id]);
        if( !$info ) return false;
        if( $this->CallUser->role == 'site' ){            
            if( $this->getSiteId() != $info['siteid'] ) logicE('非法操作');
        }
        $data['status'] = $info['status']==1?2:1;
        return D('Tags','set')->edit($id,$data);
    }
}